package com.bkgy.modular.actualTime.service;

import com.baosight.hdsdk.domain.data.HDDataConnection;
import com.baosight.hdsdk.exception.HDSdkException;
import com.bkgy.modular.actualTime.dao.ActualDao;
import com.bkgy.modular.actualTime.entity.Actual642;
import com.bkgy.modular.actualTime.entity.ActualStripHcp;
import com.bkgy.modular.alarm.dao.AlarmDao;
import com.bkgy.modular.alarm.entity.Alarm;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.SDKUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ActualService642 {
    @Resource
    private SDKUtils sdkUtils;

    @Resource
    private AlarmDao alarmDao;

    @Resource
    private ActualDao actualDao;

    public ResultData readData() {
        try {
            List<ActualStripHcp> list = actualDao.findStripHcp();
            List<Alarm> c642 = alarmDao.findAll("C642", 4.0);
            HDDataConnection connect = sdkUtils.getConnect();
            while (!connect.isConnected()) {
                connect = sdkUtils.getConnect();
            }
            Actual642 actual642 = new Actual642();
            HashMap<String, Object> map = new HashMap<>();
            HashMap<String, Integer> alarm = new HashMap<>();
            HDDataConnection finalConnect = connect;
            c642.forEach(t -> {
                Integer tagId = sdkUtils.findTagId(t.getL1_variable_name(), finalConnect.getTagManager());
                String value = sdkUtils.findDataOneById(tagId, finalConnect.getDataProvider());
                map.put(t.getL1_variable_alias(), value);
                if(NumberUtils.isNumber(value)&&(t.getL1_variable_alias().contains("hcpos")||t.getL1_variable_alias().contains("strippos"))){
                    List<Double> collect = list.stream().filter(w -> w.getL1_variable_alias().equals(t.getL1_variable_alias())).map(ActualStripHcp::getThreshold_value).sorted().collect(Collectors.toList());
                    if(StringUtils.hasText(value) &&!(Double.valueOf(value)>collect.get(0)&&Double.valueOf(value)<collect.get(1))){
                        alarm.put(t.getL1_variable_alias(),1);
                    }
                    alarm.put(t.getL1_variable_alias(),0);

                }
            });
            actual642.setMap(map);
            actual642.setAlarm(alarm);
            try {
                connect.dispose();
            } catch (HDSdkException e) {
                log.error("readData() connect.dispose()释放失败");
                e.printStackTrace();
            }
            return ResultData.ResultDataSuccess(actual642);
        } catch (Exception e) {
            log.info("sdk获取信息失败");
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取数据失败",null);
        }
    }
}
